package tgc.edu.tgq.demo.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.tgq.demo.entity.Student;
import tgc.edu.tgq.demo.entity.SysRole;
import tgc.edu.tgq.demo.entity.TbClass;
import tgc.edu.tgq.demo.repository.StudentRepository;
import tgc.edu.tgq.demo.service.StudentService;
import tgc.edu.tgq.demo.service.SysRoleService;
import tgc.edu.tgq.demo.service.TbClassService;
import tgc.edu.tgq.demo.web.form.StudentForm;

@Controller
@RequestMapping(value = "/studentmanage")
public class StuMageController {
	@Autowired
	private StudentService studentService;
	@Autowired
	private TbClassService classService; 
	@Autowired
	private SysRoleService sysRoleService;

	
	@RequestMapping(value = "/manage")
	public void list(ModelMap modelMap) {
		List<TbClass> bjs=classService.findAll();
		modelMap.put("bjs", bjs);
	}
	
	@RequestMapping(value = "/table")
	/* ModelMap传递控制方法传递数据到结果页面。类似于request的setAttribute方法的作用。 */
	public String table(Integer bjId,ModelMap modelMap) {
		List<Student> list=studentService.findByTbClassId(bjId);
			modelMap.put("list", list);
			return "studentmanage/table";
	}

	@RequestMapping(value = "/edit")
	public void edit(Integer bjId,ModelMap modelMap) {
		Student model=new Student();
		TbClass tbClass=classService.findById(bjId);
		model.setTbClass(tbClass);
		modelMap.put("model", model);
	}
	/* ModelMap传递控制方法传递数据到结果页面。类似于request的setAttribute方法的作用。 */
	
	@RequestMapping(value = "/update")
	public String update(Integer id,Integer bjId,ModelMap modelMap) {
		Student model=new Student();
		model=studentService.findById(id);
		modelMap.put("model",model);
		return "studentmanage/xg";
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(StudentForm form) {
		Student model=new Student();
		Integer id=form.getId();
		if(id!=null) {
			model=studentService.findById(id);
			BeanUtils.copyProperties(form, model);
			model.setUsername(form.getStuNo());
			studentService.save(model);
			return "OK";
		}else {
			BeanUtils.copyProperties(form, model);
			Integer bjId=form.getTbClassId();
			model.setTbClass(classService.findById(bjId));
			model.setUsername(form.getStuNo());
			List<SysRole> role=new ArrayList<SysRole>();
			role.add(sysRoleService.findById(3));
			model.setRoles(role);
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			String password=encoder.encode(form.getPassword());
			model.setPassword(password);
			studentService.save(model);
			return "OK";
		}
	}
	
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(Integer id) {
		studentService.deleteById(id);
		return "OK";
	}
	
	@RequestMapping(value = "resetpwd")
	public void resetpwd(Integer id) {
		Student stu=studentService.findById(id);
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		String password=encoder.encode("1111");
		stu.setPassword(password);
		studentService.save(stu);
	}
}
